package com.kdtech.analyse.outside;
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.analyse.tool.SelectType;
import com.kdtech.analyse.tool.SubstrLogic;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.HtmlCleaner;

public class TakungpaoNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://news.takungpao.com/mainland/focus/2012-11/1284951.html
		// http://news.takungpao.com/edu/q/2012/1122/1284982.html
		//http://www.takungpao.com/finance/content/2012-11/23/content_1390342.htm
		//http://news.takungpao.com/taiwan/shizheng/2014-05/2475612.html
		boolean bRet=false;
		String[] regex={
				"http://.*[.]takungpao[.]com/.*/[0-9]{4}/[0-9]{2,4}/[0-9]*[.]html",
				"http://.*.takungpao.com/.*/[0-9]{4}-[0-9]{2}/[0-9]+[_0-9]*.html",
				"http://.*.takungpao.com.hk/.*/[0-9]{4}-[0-9]{2}/[0-9]*.html",
				"http://.*[.]takungpao[.]com/.*/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*[.]htm" };
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("h3#title_tex"));
		tool.addParseLogic(new ParseLogic("div.gx_content>div.con_left"));
		tool.addParseLogic(new ParseLogic("div.content>h1"));
		tool.addParseLogic(new ParseLogic("div.article h1"));
		tool.addParseLogic(new ParseLogic("div.home-header-content-cont"));
		tool.addParseLogic(new ParseLogic("h1.tpk_con_tle"));
		title=tool.parse();

		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic(".left_news_info"));
		tool.addParseLogic(new ParseLogic("div#time_tex"));
		tool.addParseLogic(new ParseLogic("div.gx_content>div.con_right"));
		tool.addParseLogic(new ParseLogic("div.conZebian"));
		tool.addParseLogic(new ParseLogic("span.time"));
		tool.addParseLogic(new ParseLogic("meta[name=publishdate]",SelectType.attr,"content"));
		tool.addParseLogic(new ParseLogic("div.article_info_l"));
		date=tool.parseDate();


		/*
		 * 解析内容
		 */
		tool.addParseLogic(new ParseLogic("div#details-con"));
		tool.addParseLogic(new ParseLogic("div#sheet"));
		tool.addParseLogic(new ParseLogic("div.conTxtZyao"));
		tool.addParseLogic(new ParseLogic("div.text"));
		tool.addParseLogic(new ParseLogic("div.tpk_text"));
		content=tool.parse();

		/*
		 * 解析作者
		 */
		tool.addParseLogic(new ParseLogic("div.articleInforL",new SubstrLogic("来源：","|")));
		tool.addParseLogic(new ParseLogic("div.conZebian",new SubstrLogic("来源：","编辑：")));
		author=tool.parse();
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);

		tool.addParseLogic(new ParseLogic("div.article div.articleInfor div.articleInforL span:containsOwn(来源：) a"));
		tool.addParseLogic(new ParseLogic("div.content div.main div.mainCt div.conZebian:containsOwn(来源：)> a"));

		return meta;
	}

	


	public static void main(String[] args) throws UnsupportedEncodingException {
		String url="http://finance.takungpao.com.hk/hkstock/cjss/2015-07/3077053.html";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		TakungpaoNewsAnalyse cnhubeiNewsAnalyse=new TakungpaoNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=cnhubeiNewsAnalyse.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}

	}

	
}
